package xs.com.base;

import android.content.Context;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import xs.com.bean.ResponeTestBean;
import xs.com.bean.SoapTestBean;
import xs.com.location.utils.CRC16;
import xs.com.location.utils.SortUtil;
import xs.com.mylibrary.base.soap.SoapGsonThreadhandler;
import xs.com.mylibrary.base.soap.SoapParamsBean;
import xs.com.mylibrary.base.soap.SoapRequestBean;
import xs.com.test.TestApp;

/**
 * Created by Administrator on 2015/9/7.
 */
public abstract class SoapBaseGsonThreadHandler<T> extends SoapGsonThreadhandler<ResponeTestBean<T>> {
    public SoapBaseGsonThreadHandler(Context context) {
        super(context);
    }

    @Override
    public void successEnd(ResponeTestBean<T> obj) {
        if (obj.getCode().equals("1000")) {
            T data = obj.getData();
            successEndT(data);
        } else {
            errorEnd(obj.getError());
        }
    }

    @Override
    protected SoapRequestBean getRequestBean() {
        List<SoapParamsBean> params = new ArrayList<SoapParamsBean>();
        BaseRequestBean bean = getBaseRequestBean();
        params.add(new SoapParamsBean("ProcName", bean.getMethodName()));
        params.add(new SoapParamsBean("Key", "2B24DFA0-AB78-4DF7-954F-1F3C35ADF4FE"));
        HashMap<String, String> data = bean.getParamsMap();
        List<StringBuffer> dataBuffer = SortUtil.mySort(data);
        StringBuffer crc16 = dataBuffer.get(1).append("&Ip=" + TestApp.getIP());
        String CRC_16_Modbus = CRC16.calcCrc16(crc16.toString().getBytes()) + "";
        params.add(new SoapParamsBean("Data", dataBuffer.get(0).toString()));
        params.add(new SoapParamsBean("CRC_16_Modbus", CRC_16_Modbus));
        return new SoapTestBean(params, bean.getType());

    }

    protected abstract BaseRequestBean getBaseRequestBean();

    protected abstract void successEndT(T data);
}
